package com.tma.mobilechart.utilitys;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.PointF;
import android.util.FloatMath;
import android.view.MotionEvent;

public class ZoomMultitouch {

	public static final float DEGREES_PER_RADIAN = 57.29f;
	public static float m_FieldOfView = 30.0f;
	public static int m_PinchFlag = 0;
	public static int m_DragFlag=0;
	public static int m_GestureMode  = 0;
	public static int origwidth;
	public static int origheight;
	
	public static float m_CurrentFOV = 0.0f;
	public static float m_Zoom , m_LastZoom  = 0.0f;
	public static float m_OldDist = 0;
	public static float m_StartFOV = 0.0f;//??
	
	public static PointF m_CurrentTouchPoint = new PointF();
	public static PointF m_MidPoint = new PointF();
	public static PointF m_LastTouchPoint = new PointF();
	
	public static float getFieldOfView() 
	{
		return m_FieldOfView;
	}
	public static void setFieldOfView(float fov) 
	{
		m_FieldOfView = fov;
		m_PinchFlag = 1;
	}
	public static void setDragFlag()
	{
		m_DragFlag=1;
	}
	public static void setGestureMode(int mode)
	{
		m_GestureMode=mode;
	}
	public static void setClipping(GL10 gl, int width, int height) 
	{
		float aspectRatio;
		float zNear = .1f;
		float zFar = 2000f;
		float size;
		gl.glViewport(0, 0, width, height);

		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();

		gl.glEnable(GL10.GL_NORMALIZE);

		aspectRatio = (float) width / (float) height; 

		// Set the OpenGL projection matrix

		size = zNear * (float) (Math.tan((double) (m_FieldOfView / (DEGREES_PER_RADIAN * 2.0f))));
		
		gl.glFrustumf(-size, size, -size / aspectRatio, size / aspectRatio,
				zNear, zFar);

		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		
	}
	
	public static float spacing(MotionEvent event) 
	{
			float x = event.getX(0) - event.getX(1);
			float y = event.getY(0) - event.getY(1);
			return FloatMath.sqrt(x * x + y * y);
	}
		
	public static void midPoint(PointF point, MotionEvent event) 
	{	
			float x = event.getX(0) + event.getX(1);
			float y = event.getY(0) + event.getY(1);
			point.set(x / 2, y / 2);
	}
		
	public static boolean handlePinchGesture(MotionEvent ev) 
	{
			float minFOV = 5.0f;
			float maxFOV = 100.0f;
			float newDist = spacing(ev);
			
			m_Zoom  = m_OldDist/newDist;

			if (m_Zoom  > m_LastZoom ) 
			{
				m_LastZoom  = m_Zoom ;
			} 
			else if (m_Zoom  <= m_LastZoom ) 
			{
				m_LastZoom  = m_Zoom ;
			}
			
			m_CurrentFOV = m_StartFOV * m_Zoom ;
			m_LastTouchPoint = m_MidPoint;
			//m_GestureMode  = PINCH_GESTURE;
			ZoomMultitouch.setGestureMode(2);
			
			if (m_CurrentFOV >= minFOV && m_CurrentFOV <= maxFOV) 
			{
				ZoomMultitouch.setFieldOfView(m_CurrentFOV);
				return true;
			} 
			else
				return false;
	}
		
	public static boolean handleDragGesture(MotionEvent ev) 
	{
			m_LastTouchPoint.x = m_CurrentTouchPoint.x;
			m_LastTouchPoint.y = m_CurrentTouchPoint.y;

			m_CurrentTouchPoint.x = ev.getX();
			m_CurrentTouchPoint.y = ev.getY();

			//m_GestureMode  = DRAG_GESTURE;
			ZoomMultitouch.setGestureMode(1);
			ZoomMultitouch.setDragFlag();

			return true;
	}
}
